<?php
declare (strict_types = 1);

namespace app\controller;

use think\Request;
use app\BaseController;
use think\facade\View;
use app\middleware\CheckLogin;
use app\model\SettingUsergroup as UsergroupModel;
use app\model\SidebarNavClass as NavClassModel;
use app\model\SidebarNavDetail as NavDetailModel;

class Group extends BaseController
{
    // 注册控制器中间件
    protected $middleware = [CheckLogin::class];

    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        //
        $data = UsergroupModel::select();
        View::assign('data',$data);

        return view('index');
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //拉取大类数据
        $class = NavClassModel::order('order','asc')->select();
        View::assign('class',$class);

        //拉取小类数据并组装进大类
        $class_detail = $class;
        for ($i=0; $i < count($class); $i++) { 
            // echo $class[$i]['id']."</br>";
            $class[$i]['detail'] = NavDetailModel::where('topID',$class[$i]['id'])->order('order','asc')->select();
        }
        View::assign('class_detail',$class_detail);

        //排序数组
        for ($i=0; $i < 9; $i++) {
            $order[$i]['id'] = $i;
            $order[$i]['name'] =  UsergroupModel::where('order',$i)->value('name');
        }
        View::assign('order',$order);

        return view('create');
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        $data = request()->param();
        $result = UsergroupModel::create($data);//调用create方法写入数据库
        if($result == false){
            return 0;
        }else{
            return $result->id;
        }
    }


    /**
     * 显示编辑资源表单页.
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function edit(Request $request, $id)
    {
        if(empty($request->param('type'))){

            //拉取原来的数据,输出为json
            $data = UsergroupModel::find($id);
            View::assign('data',$data);

            //拉取大类数据
            $class = NavClassModel::order('order','asc')->select();
            View::assign('class',$class);

            //拉取小类数据并组装进大类
            $class_detail = $class;
            for ($i=0; $i < count($class); $i++) { 
                // echo $class[$i]['id']."</br>";
                $class[$i]['detail'] = NavDetailModel::where('topID',$class[$i]['id'])->order('order','asc')->select();
            }
            View::assign('class_detail',$class_detail);

            //排序数组
            for ($i=0; $i < 9; $i++) {
                $order[$i]['id'] = $i;
                $order[$i]['name'] =  UsergroupModel::where('order',$i)->value('name');
            }
            View::assign('order',$order);

            return view('edit');

        }elseif($request->param('type') == 'data'){
            //拉取原来的数据,输出为json
            $data = UsergroupModel::find($id);
            $data['nav_class'] = explode(",",$data['nav_class']);
            $data['nav_detail'] = explode(",",$data['nav_detail']);
            return json($data);
        }
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        $data = request()->param();
        $result = UsergroupModel::update($data);//调用create方法写入数据库
        if($result == false){
            return 0;
        }else{
            return $result->id;
        }
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //删除
        $user = UsergroupModel::find($id);//删除id号
        $result = $user->delete();//确认删除结果，返回布尔值
        if($result){
            return "ok";//成功则返回OK
        }else{
            return "false";//失败则返回false
        }
    }
}
